/*
 * @Description: vscode自带注释
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2021-04-11 11:29:47
 * @LastEditors: lhl
 * @LastEditTime: 2021-06-20 21:10:41
 */
import { createI18n } from 'vue-i18n' // import from runtime only
import { getLanguage } from '../utils/cookies'

import elementEnLocale from 'element-plus/lib/locale/lang/en'
import elementZhLocale from 'element-plus/lib/locale/lang/zh-cn'
import elementJaLocale from 'element-plus/lib/locale/lang/ja'
import elementTwLocale from 'element-plus/lib/locale/lang/zh-tw'

// User defined lang
// User defined lang
import enUsLocale from './en_US/index'
import zhCnLocale from './zh_CN/index'
import jaJpLocale from './ja_JP/index'
import zhTwLocale from './zh_TW/index'

const messages = {
  en: {
    ...enUsLocale,
    ...elementEnLocale
  },
  'zh-cn': {
    ...zhCnLocale,
    ...elementZhLocale
  },
  ja: {
    ...jaJpLocale,
    ...elementJaLocale
  },
  'tw-cn': {
    ...zhTwLocale,
    ...elementTwLocale
  }
}

export const getLocale = () => {
  const cookieLanguage = getLanguage()
  if (cookieLanguage) {
    return cookieLanguage
  }
  const language = navigator.language.toLowerCase()
  const locales = Object.keys(messages)
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      return locale
    }
  }

  // Default language is english
  return 'zh'
}

const i18n = createI18n({
  locale: getLocale(),
  messages: messages
})

export default i18n
